import http from '@src/util/http'

const OUTSIDE = '/api/report/outside'
const ES = '/api/elasticsearch/outside'
const DATAWARE =  '/api/dataware/outside';
const POINT = '/api/point';

/** ********************* 接口类型验证 ********************/

// 获取统计指标
interface Field {
    type: Number,
    taskType: String
}

// 工单完成时效统计
interface FinishedTimeCount {
    taskStatusType: Number,
    consumeType: Number,
    taskTypeId: String,
    userIds: Array<String>,
    tagIds: Array<String>,
    timeStart: String,
    timeEnd: String,
}
// 人员
interface TeamUser {
    state: String,
    teamId: String
}

// 工单完成时效统计-分页列表
interface FinishedTimeCountPage {
    taskStatusType: Number,
    consumeType: Number,
    taskTypeId: String,
    userIds: Array<String>,
    tagIds: Array<String>,
    timeStart: String,
    timeEnd: String,
    timeInterval: Number,
    pageSize: Number,
    pageNum: Number  
}

//  产品报修率统计-工单 统计
interface Repaired {
  timeStart: String,
  timeEnd: String,
  taskTypeId: String,
  sorts?: Array<Object>
}

// 人员工作量统计
interface CustomerCount{
  timeStart: String,
  timeEnd: String,
  tagId: Array<String>,
  ignoreNull: Boolean
}

// 人员工作量统计分页
interface CustomerPage {
  timeStart: String,
  timeEnd: String,
  tagId: Array<String>,
  userId: String,
  ignoreNull: Boolean
}

// 工单：根据关键字查询工单信息
interface Aggregation {
  templateId?: String,
  aggregationStr: String,
  sorts?: Array<Object>,
  systemConditions: Array<Object>,
}

// 工单：根据关键字查询工单信息-分页
interface AggregationPage {
  templateId: String,
  aggregationStr: String,
  sorts?: Array<Object>,
  systemConditions: Array<Object>,
  aggregationValue: String,
  timeInterval: String,
  page: Number,
  pageSize: Number
}

// 创建自定义报表
interface ReportCreate {
  type: Number,
  chartName: string,
  chartDesc: string,
  aggregationStr: string,
  viewId: Number,
  chartType: string,
  taskType: Number
}

// 获取指定的的自定义报表
interface ReportGet{ 
  customId: String
}

// 创建视图
interface CreateView {
  customId: any,
  searchModel: Object,
  type: Number
}

// 备件品类
interface SparepartParams {
  repertoryIdList: string[],
  page: number,
  pageSize: number,
  keyWord: string
}

// 区域地址
interface AreaCodeParams {
  areaCode: string
}

// 区域地址
interface grayFunctionParams {
  code: string
}

// 获取人员接口
interface teamUserParams {
  queryAll: Boolean, // 是否查询已删除人员 true 查 false 不查
  userName: string,
  pageNum: number,
  pageSize: number,
  teamId?: string
}

// 保存统计报表-自定义报表
interface reportThemeParams {
  parentName: string,
  reportName: string,
  reportType: Object
}

// 是否有报表权限
interface reportAuthParams {
  authKey: string
}
// 保存统计报表-自定义报表
interface reportExportParams {
  aggregationStr: string,
  templateId?: string,
  [x: string]: any
}

/** ********************* API ************************/

// 报表配置，之前是在jsp中返回的
const config = () => {
  return http.get(`${OUTSIDE}/stats/config`)
}

// 工单类型
const taskType = () => {
  return http.get(`${OUTSIDE}/stats/staff/taskType`)
}
const dataWareTaskType = () => {
  return http.get(`${DATAWARE}/stats/staff/taskType`)
}

// 带有权限的工单类型
const taskTypeByTag = () => {
  return http.get(`${OUTSIDE}/stats/staff/taskTypeByTag`)
}

// 获取统计指标
const field = (params: Field) => {
  return http.get(`${OUTSIDE}/custom/report/statistics/field`, params)
}

// 工单完成时效统计
const finishedTimeCount = (params: FinishedTimeCount) => {
  return http.post(`${OUTSIDE}/stats/task/finishedTimeCount`, params)
}

// 工单未完成时效统计
const unFinishedTimeCount = (params: FinishedTimeCount) => {
  return http.post(`${OUTSIDE}/stats/task/unFinishedTimeCount`, params)
}

// 人员
const teamUser = (params: TeamUser) => {
  return http.post(`${OUTSIDE}/stats/staff/teamUser`, params)
} 

// 获取所有的自定义报表
const getAll = () => {
  return http.get(`${OUTSIDE}/custom/report/getAll`)
}

// 工单完成时效统计-分页列表
const finishedTimeCountPage = (params: FinishedTimeCountPage) => {
  return http.post(`${OUTSIDE}/stats/task/finishedTimeCount/page`, params)
}

// 工单未完成时效统计-分页数据
const unFinishedTimeCountPage = (params: FinishedTimeCountPage) => {
  return http.post(`${OUTSIDE}/stats/task/unFinishedTimeCount/page`, params)
}

// 产品报修率统计-工单 统计
const repaired = (params: Repaired) => {
  return http.post(`${OUTSIDE}/stats/product/repaired`, params)
}

// 产品报修率统计-工单-分页数据
const repairedPage = (params: FinishedTimeCountPage) => {
  return http.post(`${OUTSIDE}/stats/product/repaired/page`, params)
}

// 产品类型
const productType = () => {
  return http.get(`${OUTSIDE}/stats/productType`)
}

// 人员工作量统计
const customerCount = (params: CustomerCount) => {
  return http.post(`${OUTSIDE}/stats/staff/customer/count`, params)
}

// 人员工作量统计分页
const customerPage = (params: CustomerPage) => {
  return http.post(`${OUTSIDE}/stats/staff/customer/page`, params)
}

// 工单：根据关键字查询工单信息
const aggregation = (params: Aggregation) => {
  return http.post(`${ES}/es/task/custom/aggregation`, params)
}

// 工单：根据关键字查询工单信息-分页
const aggregationPage = (params: AggregationPage) => {
  return http.post(`${ES}/es/task/custom/aggregation/page`, params)
}

// 客户：根据关键字查询客户信息
const customerCustomer = (params: Aggregation) => {
  return http.post(`${ES}/es/customer/aggregation`, params)
}

// 客户：根据关键字查询工单信息-分页
const aggregationData = (params: AggregationPage) => {
  return http.post(`${ES}/es/customer/aggregation/data`, params)
}

// 产品：根据关键字查询
const aggregationProductData = (params: AggregationPage) => {
  return http.post(`${ES}/es/product/report/aggregation/data`, params)
}


// 自定义应用：根据关键字查询
const aggregationCustomData = (params: AggregationPage) => {
  return http.post(`/api/paas/outside/report/es/aggregation/data`, params)
}


// 创建自定义报表
const reportCreate = (params: ReportCreate) => {
  return http.post(`${OUTSIDE}/custom/report/create`, params) 
}

// 获取指定的的自定义报表
const reportGet = (params: ReportGet) => {
  return http.get(`${OUTSIDE}/custom/report/get`, params) 
}

// 删除自定义报表
const reportDelete = (params: ReportGet) => {
  return http.post(`${OUTSIDE}/custom/report/delete`, params, false) 
}

// 修改自定义报表
const reportUpdate = (params: ReportCreate) => {
  return http.post(`${OUTSIDE}/custom/report/update`, params) 
}

// 创建自定义视图
const createView = (params: CreateView) => {
  return http.post(`${OUTSIDE}/custom/report/createView`, params) 
}

// 获取自定义视图
const getView = (params: ReportGet) => {
  return http.get(`${OUTSIDE}/custom/report/getView`, params) 
}

// 修改自定义视图
const updateView = (params: CreateView) => {
  return http.post(`${OUTSIDE}/custom/report/updateView?customId=${params.customId}`, params) 
}

// 管理报表 获取仓库列表
const getRepertory = () => {
  return http.get('/api/dataware/outside/materialTheme/getTenantRepertoryList') 
}

// 管理报表 获取备件品类列表
const getSparepart = (params: SparepartParams) => {
  return http.post('/api/dataware/outside/materialTheme/getTenantSparepartList', params)
}

// 管理报表 获取服务类型列表
const getServiceTypeList = () => {
  return http.get('/api/dataware/outside/financial/getServiceTypeList')
}

// 管理报表 获取服务类型列表
const getCnareaList = (params: AreaCodeParams) => {
  return http.post('/api/dataware/outside/cnarea/getCnareaList', params);
}

// 管理报表 带权限控制的人员接口
const getPcTeamUser = (params: teamUserParams) => {
  return http.post('/api/dataware/outside/user/pcTeamUser', params);
}

// 获取统计报表
const getReportThemeTree = () => {
  return http.get('/api/dataware/outside/reportTheme/getReportThemeTree/STATISTICALREPORT');
}

// 保存统计报表-自定义报表
const insertReportTheme = (params: reportThemeParams) => {
  return http.post(`/api/dataware/outside/reportTheme/insertReportTheme`, params);
}

// 保存统计报表-自定义报表
const deleteReportTheme = (customId: string) => {
  return http.get(`/api/dataware/outside/reportTheme/deleteReportTheme/${customId}`);
}

// 灰度
const grayFunction = () => {
  return http.get(`/api/customer/outside/quality/grayFunction?code=dashBoardAirp`);
}

// 北极数据
const getBeiJiToken = () => {
  return http.get(`${DATAWARE}/user/getLoginToken`);
}

// 是否有报表权限
const getReportAuth = (params: reportAuthParams) => {
  return http.get(`${POINT}/outside/auth/report`, params);
}

// 某个报表模块是否有权限
const getAuthorityForKey = (reportKey: String) => {
  return http.get(`/api/dataware/outside/authority/getAuthorityForKey/${reportKey}/1`); // 1为系统报表
}
// 自定义报表-表格的导出功能-工单
const taskReportExport = (params: reportExportParams) => {
  return http.post(`/api/report/outside/custom/report/task/custom/export`, params);
}

// 自定义报表-表格的导出功能-客户
const customReportExport = (params: reportExportParams) => {
  return http.post(`/api/report/outside/custom/report/customer/custom/export`, params);
}

// 新版数据大屏灰度接口
const getDataScreenGray = (params: reportExportParams) => {
  return http.get(`${DATAWARE}/gray/allInfo`, params);
}

// 客户满意度统计报表-客户满意度筛选维度
function queryStatsCustomerType(params = {}) {
  return http.post('/api/dataware/outside/satisfaction/customer/type', params, true)
}

// 客户满意度统计报表-满意度上方客户统计
function queryStatsCustomerCount(params = {}) {
  return http.post('/api/dataware/outside/satisfaction/customer/count', params, true)
}

// 获取客户满意度统计报表-满意度数据统计饼图
function queryStatsCustomerChart(params = {}) {
  return http.post('/api/dataware/outside/satisfaction/customer/chart', params, true)
}

// 获取客户满意度统计报表-满意度数据统计列表
function queryStatsCustomerList(params = {}) {
  return http.post('/api/dataware/outside/satisfaction/customer/list', params, true)
}

// 获取客户满意度统计报表-报表导出
function satisfactionReportExport(params = {}) {
  return http.post('/excels/satisfaction/Report/export', params, true)
}
// 产品 根据关键字查询自定义应用信息（paas）
const productAggregation = (params: Aggregation) => {
  return http.post(`/api/elasticsearch/outside/es/product/report/aggregation`, params)
}

// 自定义应用：根据关键字查询自定义应用信息（paas）
const paasAggregation = (params: Aggregation) => {
  return http.post(`/api/paas/outside/report/es/aggregation`, params)
}


const aggregateReportExport = (params = {}, type: string) => {
  let apiUrl = ''
  switch (type) {
    case 'task':
      apiUrl = '/excels/task/custom/export'
      break;
    case 'product':
      apiUrl = '/excels/product/custom/export'
      break;
    case 'client':
      apiUrl = '/excels/customer/custom/export'
      break;
    case 'custom':
      apiUrl = '/excels/paas/custom/export'
      break;
  }
  return http.post(apiUrl, params);
}
// 自定义报表入口展示
const getAddReportAuthority = (params: any) => {
	return http.get(`${DATAWARE}/authority/getAddReportAuthority`);
}
const getReportAggregateAuth = (params = {}) => {
  return http.get(`/api/report/outside/stats/config`, params)
}

const checkSatisfactionGray = (params = {}) => {
  return http.post(`/api/customer/outside/satisfactionConfig/checkSatisfactionGray`, params)
}

// 客户信息统计图表接口
const getAnalysisData = (params: Aggregation) => {
  return http.post(`${DATAWARE}/statistics/customer/analysis`, params)
}
// 客户信息统计图表加载更多接口
const getAnalysisNextpage = (params: Aggregation) => {
  return http.post(`${DATAWARE}/statistics/customer/analysis/nextpage`, params)
}

// 人员地图-获取用户未完成工单接口
const getUserUnFinishedTask = (userId: string) => {
  return http.get(`/api/task/outside/pc/task/getUserUnFinishedTask/${userId}`)
}

// 霍尼韦尔：地图统计
const getHoneywellMapData = (params: Aggregation) => {
  return http.post(`${POINT}/outside/honeywell/statistics/map`, params)
}

export {
  config, 
  taskType, 
  taskTypeByTag, 
  field, 
  updateView, 
  createView, 
  getView, 
  reportUpdate, 
  finishedTimeCount, 
  teamUser, 
  reportDelete, 
  getAll, 
  finishedTimeCountPage, 
  unFinishedTimeCount, 
  unFinishedTimeCountPage, 
  repaired, 
  repairedPage, 
  productType, 
  customerCount, 
  customerPage, 
  aggregation, 
  aggregationPage, 
  customerCustomer, 
  aggregationData, 
  reportCreate, 
  reportGet, 
  getRepertory, 
  getSparepart, 
  getServiceTypeList, 
  getCnareaList, 
  getPcTeamUser,
  getReportThemeTree,
  insertReportTheme,
  deleteReportTheme,
  grayFunction,
  getBeiJiToken,
  getReportAuth,
  getAuthorityForKey,
  taskReportExport,
  customReportExport,
  getDataScreenGray,
  queryStatsCustomerType,
  queryStatsCustomerCount,
  queryStatsCustomerChart,
  queryStatsCustomerList,
  satisfactionReportExport,
  paasAggregation,
  productAggregation,
  aggregationProductData,
  aggregationCustomData,
  aggregateReportExport,
  getReportAggregateAuth,
  checkSatisfactionGray,
  getAnalysisData,
  getAnalysisNextpage,
  getAddReportAuthority,
  getUserUnFinishedTask,
  getHoneywellMapData,
}